
#python3 环境，需要执行pip3 install requests
import requests
import json
import ssl
import smtplib
import time
import datetime
from email.mime.text import MIMEText
from email.header import Header


#fir请求应用最新信息
appId = '606a8d2923389f7743fb9513'

apiToken = '79f12812ce966f0216af0245832786d4'
firBaseApi = 'http://api.bq04.com/apps'

#版本更新信息请求
appVersionApi = firBaseApi + '/latest/' + appId + '?api_token=' + apiToken
appVersionResponse = requests.get(appVersionApi)
appVersionResponseJsonObj = json.loads(appVersionResponse.text)
update_url = appVersionResponseJsonObj['update_url']
appName = appVersionResponseJsonObj['name'];
appBuildNum = appVersionResponseJsonObj['build']
appVersion = appVersionResponseJsonObj['versionShort']
appChangeLog = appVersionResponseJsonObj['changelog']
appUpdateTime = appVersionResponseJsonObj['updated_at']
appUpdateTimeStr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(appUpdateTime))


#应用信息请求，获取最新版本的直接下载链接
appInfoApi = firBaseApi + '/' + appId + '?api_token=' + apiToken
appInfoResponse = requests.get(appInfoApi)
appInfoJsonObj = json.loads(appInfoResponse.text)
appReleaseId = appInfoJsonObj['master_release_id']

appFullDownLoadUrl = update_url + '?' + 'release_id=' + appReleaseId 



# 第三方 SMTP 服务
mail_host = 'smtp.163.com'  #设置服务器
mail_user = 'stonemover@163.com'    #用户名
mail_pass = 'ZDZMMOSEVBKIXQAR'   #口令 


sender = 'stonemover@163.com'
receivers = ['776299940@qq.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱

finishTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
emailContent = 'safari浏览器打开进行下载,下载教程参考fir官方文档，iOS16需要打开开发者模式\n下载地址：' + appFullDownLoadUrl + '\nApp版本号：' + appVersion + '\nApp构建号：' + appBuildNum + '\n更新时间:' + appUpdateTimeStr + '\n更新内容:' + appChangeLog 

message = MIMEText(emailContent, 'plain', 'utf-8')
message['From'] = Header("stonemover@163.com", 'utf-8')
message['To'] =  Header("776299940@qq.com", 'utf-8')

subject = appName + 'iOS adhoc 构建完成'
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP() 
    smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    print ("邮件发送成功")
    print (emailContent)
except smtplib.SMTPException:
    print ("Error: 无法发送邮件")


# from_name="stonemover"
# from=stonemover@163.com
# to=776299940@qq.com
# sendTime=date
# #邮箱用户名
# email_username=stonemover@163.com
# #邮箱SMTP授权码
# email_password=ZDZMMOSEVBKIXQAR
# #smtp服务器地址
# email_smtphost=smtp.163.com
# #邮件名
# email_title="优选好股-adhoc-打包成功"
# #邮件内容
# email_content="Safari浏览器打开链接下载，下载前请刷新该网页，确认上传时间以免下载错误版本。\n该版本下载地址：http://hey.scandown.com/z4sw；上传时间：${sendTime}"
# #邮件主题
# email_subject="优选好股-adhoc-打包成功"
# /usr/local/bin/sendEmail -f ${from} -t ${to} -s ${email_smtphost} -u ${email_title} -xu ${email_username} -xp ${email_password} -m ${email_content} -o message-charset=utf-8 -o tls=no
